import dayjs from "dayjs";
function objCopy(obj, hash = new WeakMap()) {
	if (obj === null || typeof obj !== "object") return obj;
	if (obj instanceof Date) return new Date(obj);
	if (obj instanceof RegExp) return new RegExp(obj);
	if (hash.has(obj)) return hash.get(obj); // 解决循环引用问题

	const clone = Array.isArray(obj) ? [] : {};
	hash.set(obj, clone);

	for (let key in obj) {
		if (obj.hasOwnProperty(key)) {
			clone[key] = objCopy(obj[key], hash); // 修复：去掉 this
		}
	}

	return clone;
}
export default {
	objCopy,
	// 获取上个月方法
	lastMonthList(formatString = "YYYY-MM-DD") {
		// 获取当前日期
		const currentDate = dayjs();

		// 获取上个月的第一天
		const firstDayOfLastMonth = currentDate
			.subtract(1, "month")
			.startOf("month")
			.format(formatString);
		// 获取上个月的最后一天
		const lastDayOfLastMonth = currentDate
			.subtract(1, "month")
			.endOf("month")
			.format(formatString);
		return [firstDayOfLastMonth, lastDayOfLastMonth];
	},
	// 获取本月
	currentMonthRange(formatString = "YYYY-MM-DD") {
		const today = dayjs();

		// 获取本月第一天
		const firstDayOfMonth = today.startOf("month").format(formatString);
		// 获取本月最后一天
		const lastDayOfMonth = today.endOf("month").format(formatString);

		return [firstDayOfMonth, lastDayOfMonth];
	},
	// 获取本周
	currentWeekRange(formatString = "YYYY-MM-DD") {
		const today = dayjs();
		const startOfWeek = today
			.startOf("week")
			.add(1, "day")
			.format(formatString); // 周一
		const endOfWeek = today
			.endOf("week")
			.subtract(1, "day")
			.add(1, "day")
			.format(formatString); // 周日

		return [startOfWeek, endOfWeek];
	},
	// 获取昨日的开始和结束时间
	yesterdayRange(formatString = "YYYY-MM-DD") {
		const yesterday = dayjs().subtract(1, "day");

		const startOfYesterday = yesterday.startOf("day").format(formatString); // 昨日 00:00:00
		const endOfYesterday = yesterday.endOf("day").format(formatString); // 昨日 23:59:59

		return [startOfYesterday, endOfYesterday];
	}
};
